package com.smfx.yxt.excel.tags;

import java.util.StringTokenizer;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.smfx.yxt.excel.ExcelUtils;

/**
 * *
 * <p>
 * <b>PageTag </b> is a class which parse the #page tag Because a bug in POI, you must place a split char in your sheet
 * near the #page tag
 * </p>
 * #page [varibale [start with pageNo]]
 * 
 */
public class PageTag implements ITag {
	public static final String KEY_PAGE = "#page";

	public int[] parseTag(HSSFSheet sheet, HSSFRow curRow, HSSFCell curCell) {
		sheet.setRowBreak(curRow.getRowNum());
		String cellstr = curCell.getRichStringCellValue().getString();
		int pos = 0;
		StringTokenizer st = new StringTokenizer(cellstr, " ");
		String variable = "pageIndex";
		Integer start = 1;
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			if (pos == 1) {
				variable = token;
			}
			else if (pos == 4) {
				start = Integer.parseInt(token);
			}
			pos++;
		}

		if (variable != null) {
			Integer page = (Integer) ExcelUtils.getValue(variable);
			if (null == page) {
				page = new Integer(start);
			}
			else {
				page = page + 1;
			}
			ExcelUtils.addValue(variable, page);
		}
		curCell.setCellValue(new HSSFRichTextString());
		return new int[] { 0, 0, 0 };
	}

	public String getTagName() {
		return KEY_PAGE;
	}

	public boolean hasEndTag() {
		return false;
	}
}
